//
//  AllocateBulidingHeaderView.m
//  QRMaster
//
//  Created by myApple on 15/6/23.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

#import "AllocateBulidingHeaderView.h"

@interface AllocateBulidingHeaderView ()
{
    UIButton * imgBtn;
    UILabel * bulidingNameLabel;
    UILabel * numberSelectFloorOfBulidingLabel;
//    UISwitch * selectSwitch;
    UIView * lineView;
}

@end

@implementation AllocateBulidingHeaderView

+(instancetype)headerWithTableView:(UITableView *)tableView
{
    static NSString * indentifier = @"header";
    AllocateBulidingHeaderView * headerView = (AllocateBulidingHeaderView *)[tableView dequeueReusableCellWithIdentifier:indentifier];
    if (headerView == nil) {
        headerView = [[AllocateBulidingHeaderView alloc]initWithReuseIdentifier:indentifier];
    }
    return headerView;
}


#pragma mark -- 构造方法中为控件设置的frame是无效的
-(id)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        
        lineView = [[UIView alloc]init];
        lineView.backgroundColor = RGBA(237, 238, 241, 1);
        [self addSubview:lineView];
        
        imgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [imgBtn setBackgroundImage:[UIImage imageNamed:@"ic_right"] forState:0];
        [imgBtn addTarget:self action:@selector(ClickBtn) forControlEvents:UIControlEventTouchUpInside];
        imgBtn.imageView.contentMode = UIViewContentModeCenter;
        imgBtn.imageView.layer.masksToBounds = YES;
        [self addSubview:imgBtn];
        
        bulidingNameLabel = [[UILabel alloc]init];
        bulidingNameLabel.font = [UIFont systemFontOfSize:16.0];
        [self addSubview:bulidingNameLabel];
        
        
        numberSelectFloorOfBulidingLabel = [[UILabel alloc]init];
        numberSelectFloorOfBulidingLabel.font = [UIFont systemFontOfSize:12.0];
        numberSelectFloorOfBulidingLabel.textAlignment = NSTextAlignmentRight;
        [self addSubview:numberSelectFloorOfBulidingLabel];
        
        _selectSwitch = [[UISwitch alloc]init];
        _selectSwitch.on = NO;
        [_selectSwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_selectSwitch];
        
        UITapGestureRecognizer * headerViewTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(ClickBtn)];
        [self addGestureRecognizer:headerViewTap];
    }
    
    return self;
}


-(void)ClickBtn
{
    self.bmodel.open = !self.bmodel.open;
    if (self.bmodel.open) {
         imgBtn.selected = YES;
    }
    else
    {
         imgBtn.selected = NO;
    }
    if (_delegate && [_delegate respondsToSelector:@selector(AllocateBulidingHeaderViewDidClickHeaderView:)]) {
        [_delegate AllocateBulidingHeaderViewDidClickHeaderView:self];
    }
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    lineView.frame = CGRectMake(0, 0, self.frame.size.width, 1);
    
    imgBtn.frame = CGRectMake(20, 15, 20, 20);
    
    bulidingNameLabel.frame = CGRectMake(45, 15, self.frame.size.width - 65 - 100, 20);
    
    numberSelectFloorOfBulidingLabel.frame = CGRectMake(self.frame.size.width - 120, 15, 60, 20);
    
    _selectSwitch.frame = CGRectMake(self.frame.size.width - 60, 10, 40, 30);
}


-(void)switchAction:(UISwitch *)changeSwitch
{
    if (!_selectSwitch.on) {
         self.bmodel.floorSelect = 0;
    }
    else
    {
        self.bmodel.floorSelect = self.bmodel.sumRoom;
    }
    if (_delegate && [_delegate respondsToSelector:@selector(AllocateBulidingHeaderViewDidClickSwitch:)]) {
        [_delegate AllocateBulidingHeaderViewDidClickSwitch:self];
    }
}

-(void)didMoveToSuperview
{
    if (self.bmodel.open) {
         [imgBtn setBackgroundImage:[UIImage imageNamed:@"ic_downn"] forState:0];
    }
}

-(void)setBmodel:(AllocateBulidingModel *)bmodel
{
    _bmodel = bmodel;
    bulidingNameLabel.text = bmodel.bulidingName;
    if (_comeFrom) {
        numberSelectFloorOfBulidingLabel.text = [NSString stringWithFormat:@"%d%@",(int)bmodel.sumRoom,NSLocalizedString(@"room", nil)];
    }else
    {
         numberSelectFloorOfBulidingLabel.text = [NSString stringWithFormat:@"%d/%d",(int)bmodel.floorSelect,(int)bmodel.sumRoom];
    }
    
    if (bmodel.floorSelect > 0) {
        _selectSwitch.on = YES;
    }
}

-(void)setComeFrom:(BOOL)comeFrom
{
    _comeFrom = comeFrom;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
